//
//  QuestionCollectionViewController.m
//  InTime
//
//  Created by zhangjia on 15/9/8.
//  Copyright (c) 2015年 ClownYoJoy. All rights reserved.
//

#import "QuestionCollectionViewController.h"
#import "HeaderView.h"
#import "QuestionCell.h"
#import "QuestionModel.h"


#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height



@interface QuestionCollectionViewController ()
//存储model的数组
@property (strong, nonatomic) NSMutableArray *array;

@end

@implementation QuestionCollectionViewController

static NSString * const reuseIdentifier = @"QuestionCell";
static NSString *heaer = @"header";
//数据懒加载，上提加载更多需要用到
//(懒加载的使用场景：当我不知道什么时候创建的时候，使用懒加载)
//- (NSMutableArray *)array{
//    if (!_array) {
//        self.array = [NSMutableArray array];
//    }
//    return _array;
//}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.array = [NSMutableArray array];

    //注册Header
    
    [self analysisData];
    // Do any additional setup after loading the view.
}


- (void)analysisData {
    //网络请求
    //动态获取当天时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    //给他一个格式
    formatter.dateFormat = @"yyyyMMdd";
    formatter.timeZone = [NSTimeZone systemTimeZone];
    NSString *date = [formatter stringFromDate:[NSDate date]];
    
    //http://news-at.zhihu.com/api/4/stories/latest?client=0
    NSString *urlStr = [NSString stringWithFormat:@"http://news-at.zhihu.com/api/4/stories/latest?client=0"];
    
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (connectionError) {
            NSLog(@"网络错误");
        }else{
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            //解析
            for (NSDictionary *stories in dic[@"stories"]) {
                QuestionModel *model = [[QuestionModel alloc] init];
                [model setValuesForKeysWithDictionary:stories];
                [self.array addObject:model];
                NSLog(@"%@ %@ %.0f", model.title, model.ID, model.ga_prefix);
            }
            //把每个分区的array 存到外层array里面;
        }
        [self.collectionView reloadData];
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return [self.array count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    QuestionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.model = self.array[indexPath.row];
    cell.image.layer.cornerRadius = 100;
    cell.image.layer.masksToBounds = YES;
    return cell;
}


#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
